define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/shop/shop/index',
                    //add_url: 'mall/shop/shop/add',
                    edit_url: 'mall/shop/shop/edit',
                    //del_url: 'mall/shop/shop/del',
                    multi_url: 'mall/shop/shop/multi',
                    table: 'mall_shop_shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'shopSn', title: __('Shopsn')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'areas', title: __('Areas'), visible:false},
                        //{field: 'areaId', title: __('Areaid')},
                        {field: 'isSelf', title: __('Isself'), visible:false},
                        {field: 'name', title: __('Name')},
                        {field: 'shopkeeper', title: __('Shopkeeper'), visible:false},
                        {field: 'telephone', title: __('Telephone'), visible:false},
                        {field: 'shopCompany', title: __('Shopcompany'), visible:false},
                        {field: 'shopimg', title: __('Shopimg'), formatter: Table.api.formatter.image},
                        {field: 'shopTel', title: __('Shoptel')},
                        {field: 'shopQQ', title: __('Shopqq')},
                        {field: 'shopWangWang', title: __('Shopwangwang'), visible:false},
                        {field: 'shopAddress', title: __('Shopaddress'), visible:false},
                        {field: 'bankId', title: __('Bankid'), visible:false},
                        {field: 'bankNo', title: __('Bankno'), visible:false},
                        {field: 'bankUserName', title: __('Bankusername'), visible:false},
                        {field: 'invoiceis', title: __('Invoiceis'), visible:false},
                        {field: 'invoiceRemarks', title: __('Invoiceremarks'), visible:false},
                        {field: 'serviceStartTime', title: __('Servicestarttime'), visible:false},
                        {field: 'serviceEndTime', title: __('Serviceendtime'), visible:false},
                        {field: 'freight', title: __('Freight'), visible:false},
                        {field: 'state', title: __('State'), visible:false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.flag},
                        {field: 'statusDesc', title: __('Statusdesc'), visible:false},
                        {field: 'createtime', title: __('Create time'), operate:'RANGE', addclass:'datetimerange', visible:false},
                        {field: 'money', title: __('money'), operate:'BETWEEN', visible:false},
                        {field: 'lockMoney', title: __('Lockmoney'), operate:'BETWEEN', visible:false},
                        {field: 'noSettledOrderNum', title: __('Nosettledordernum'), visible:false},
                        {field: 'noSettledOrderFee', title: __('Nosettledorderfee'), operate:'BETWEEN', visible:false},
                        {field: 'paymentMoney', title: __('Paymentmoney'), operate:'BETWEEN', visible:false},
                        //{field: 'bankAreaId', title: __('Bankareaid')},
                        {field: 'applyStep', title: __('Applystep'), visible:false},
                        {field: 'shopNotice', title: __('Shopnotice'), visible:false},
                        {field: 'rechargeMoney', title: __('Rechargemoney'), operate:'BETWEEN', visible:false},
                        {field: 'longitude', title: __('Longitude'), operate:'BETWEEN', visible:false},
                        {field: 'latitude', title: __('Latitude'), operate:'BETWEEN', visible:false},
                        {field: 'mapLevel', title: __('Maplevel'), visible:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                title: __('Detail'),
                                icon: 'fa fa-eye',
                                classname: 'btn btn-info btn-xs btn-detail',
                                extend: "target='_blank'",
                                url: '/addons/mall/shop/detail'
                            }],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $("#c-businessArea1").trigger("fa.event.businessAreaupdated",'edit');
            $("#c-category1").trigger("fa.event.areaidsupdated",'edit');
            $("#c-bankAreaIds1").trigger("fa.event.bankAreaIdsupdated",'edit');
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                //四级地区联动参数传递
                $("#c-businessArea2").data("params", function (obj) {
                    return {custom: {pid: $("#c-businessArea1").val()}};
                });
                $("#c-businessArea3").data("params", function (obj) {
                    return {custom: {pid: $("#c-businessArea2").val()}};
                });
                $("#c-businessArea4").data("params", function (obj) {
                    return {custom: {pid: $("#c-businessArea3").val()}};
                });

                $("#c-category2").data("params", function (obj) {
                    return {custom: {pid: $("#c-category1").val()}};
                });
                $("#c-category3").data("params", function (obj) {
                    return {custom: {pid: $("#c-category2").val()}};
                });
                $("#c-category4").data("params", function (obj) {
                    return {custom: {pid: $("#c-category3").val()}};
                });

                $("#c-bankAreaIds2").data("params", function (obj) {
                    return {custom: {pid: $("#c-bankAreaIds1").val()}};
                });
                $("#c-bankAreaIds3").data("params", function (obj) {
                    return {custom: {pid: $("#c-bankAreaIds2").val()}};
                });
                $("#c-bankAreaIds4").data("params", function (obj) {
                    return {custom: {pid: $("#c-bankAreaIds3").val()}};
                });
                //一级地区选定，清除二、三、四级选项
                $(document).on("change", "#c-businessArea1", function () {
                    require(['selectpage'], function () {
                        $('#c-businessArea2').selectPageClear();
                        $('#c-businessArea3').selectPageClear();
                        $('#c-businessArea4').selectPageClear();
                        $("#c-businessAreaPath").val($('#c-businessArea1').val());
                    });
                });
                $(document).on("change", "#c-category1", function () {
                    require(['selectpage'], function () {
                        $('#c-category2').selectPageClear();
                        $('#c-category3').selectPageClear();
                        $('#c-category4').selectPageClear();
                        $("#c-areaIds").val($('#c-category1').val());
                    });
                });
                $(document).on("change", "#c-bankAreaIds1", function () {
                    require(['selectpage'], function () {
                        $('#c-bankAreaIds2').selectPageClear();
                        $('#c-bankAreaIds3').selectPageClear();
                        $('#c-bankAreaIds4').selectPageClear();
                        $("#c-bankAreaIds").val($('#c-bankAreaIds1').val());

                    });
                });
                //二级地区选定，清除三、四级选项
                $(document).on("change", "#c-businessArea2", function () {
                    require(['selectpage'], function () {
                        $('#c-businessArea3').selectPageClear();
                        $('#c-businessArea4').selectPageClear();
                        $("#c-businessAreaPath").val($("#c-businessArea1").val()+","+$("#c-businessArea2").val());
                    });
                });
                $(document).on("change", "#c-category2", function () {
                    require(['selectpage'], function () {
                        $('#c-category3').selectPageClear();
                        $('#c-category4').selectPageClear();
                        $("#c-areaIds").val( $("#c-category1").val()+","+$("#c-category2").val());
                    });
                });
                $(document).on("change", "#c-bankAreaIds2", function () {
                    require(['selectpage'], function () {
                        $('#c-bankAreaIds3').selectPageClear();
                        $('#c-bankAreaIds4').selectPageClear();
                        $("#c-bankAreaIds").val($("#c-bankAreaIds1").val()+","+$("#c-bankAreaIds2").val());

                    });
                });
                //三级地区选定，清除四级选项
                $(document).on("change", "#c-businessArea3", function () {
                    require(['selectpage'], function () {
                        $('#c-businessArea4').selectPageClear();
                        $("#c-businessAreaPath").val( $("#c-businessArea1").val()+","+$("#c-businessArea2").val()+","+$("#c-businessArea3").val());
                    });
                });
                $(document).on("change", "#c-category3", function () {
                    require(['selectpage'], function () {
                        $('#c-category4').selectPageClear();
                        $("#c-areaIds").val( $("#c-category1").val()+","+$("#c-category2").val()+","+$("#c-category3").val());
                    });
                });
                $(document).on("change", "#c-bankAreaIds3", function () {
                    require(['selectpage'], function () {
                        $('#c-bankAreaIds4').selectPageClear();
                        $("#c-bankAreaIds").val($("#c-bankAreaIds1").val()+","+$("#c-bankAreaIds2").val()+","+$("#c-bankAreaIds3").val());
                    });
                });
                //拼接四级地区选项值，用于保存
                $(document).on("change", "#c-category4", function () {
                    $("#c-businessAreaPath").val($("#c-businessArea1").val()+","+$("#c-businessArea2").val()+","+$("#c-businessArea3").val()+","+$("#c-businessArea4").val());
                });
                $(document).on("change", "#c-category4", function () {
                    $("#c-areaIds").val( $("#c-category1").val()+","+$("#c-category2").val()+","+$("#c-category3").val()+","+$("#c-category4").val());
                });
                $(document).on("change", "#c-bankAreaIds4", function () {
                    $("#c-bankAreaIds").val( $("#c-bankAreaIds1").val()+","+$("#c-bankAreaIds2").val()+","+$("#c-bankAreaIds3").val()+","+$("#c-bankAreaIds4").val());
                });
                //四级地区默认值设置（只执行一次）
                $(document).one("fa.event.businessAreaupdated", "#c-businessArea1", function () {
                    var a = $("#c-businessAreaPath").val().split(",");
                    $("#c-businessArea1").val(a[0]);
                    $("#c-businessArea2").val(a[1]);
                    $("#c-businessArea3").val(a[2]);
                    $("#c-businessArea4").val(a[3]);
                });
                $(document).one("fa.event.areaidsupdated", "#c-category1", function () {
                    var a = $("#c-areaIds").val().split(",");
                    $("#c-category1").val(a[0]);
                    $("#c-category2").val(a[1]);
                    $("#c-category3").val(a[2]);
                    $("#c-category4").val(a[3]);
                });
                $(document).one("fa.event.bankAreaIdsupdated", "#c-bankAreaIds1", function () {
                    var a = $("#c-bankAreaIds").val().split(",");
                    $("#c-bankAreaIds1").val(a[0]);
                    $("#c-bankAreaIds2").val(a[1]);
                    $("#c-bankAreaIds3").val(a[2]);
                    $("#c-bankAreaIds4").val(a[3]);
                });
            }
        }
    };
    return Controller;
});